/**
 * Created by Chen on 2016/2/9.
 */
define(function(require, exports, module){

    var $ = require('jquery');
    require('bootstrap');
    require('bootstrap.select');
    require('jquery.layer');
    require('jquery.icheck');

    var List = require('../../util/list.js');

    var admin_target = $("#admin_target").val();

    exports.loadList = function(param){
        var list = new List();
        list.options = {
            template : "list-template",
            container : "list-data",
            url : $CONFIG.base_url + "/api/topic/list",
            param : param
        };
        list.load(function () {
            $(".topic-details").on('click',function(){
                exports.loadList1({"id":$(this).attr("value") });
            })
        });
    };
    if(admin_target == 1 || admin_target == 2) {
        exports.loadList();
    }
    else {
        exports.loadList({'creatorType':0});
    }

    $(".btn-search-submit").click(function(){
        var $form = $("#search-form");

        var postData = $form.serialize();
        exports.loadList(postData);
    });


    $("#list-data").on("click",".topic-aks-for",function(){
        var data = $.parseJSON($(this).attr("data"));
        var curNum = $(this).attr("curNum");
        var total = $(this).attr("total");
        var pass = $(this).attr("pass");
        layer.confirm('是否选择该题目？', {
            btn: ['确定','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            if(pass != 3){
                layer.msg('不能选择该题目',{
                    icon: 9,
                    time: 2000
                });
            }else if(curNum == total){
                layer.msg('人数已满',{
                    icon: 9,
                    time: 2000
                });
            }
            else {
                var posting = $.post($CONFIG.base_url + "/api/topic/student/ask_for", data);
                posting.done(function (data) {
                    if (data == "true") {
                        layer.msg('选择成功', {
                            icon: 1,
                            time: 1000
                        }, function () {
                            window.location.href = $CONFIG.base_url + "/student/choice/manage";
                        });
                    }
                    if (data == "false") {
                        layer.msg('您已选过题目了', {icon: 9});
                    }
                });
            }
        }, function(){
        });
    });


    $("#list-data").on("click",".topic-del",function(){
        var data = $.parseJSON($(this).attr("data"));
        var $this = $(this);
        layer.confirm('要删除该记录？', {
            btn: ['确认','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            var posting = $.post($CONFIG.base_url + "/api/topic/del", data);
            posting.done(function(data){
                layer.msg('删除成功', {icon:9});
                $this.parents("tr").remove();
            }).fail(function() {
                layer.msg('无权限或出现错误，请联系管理员', {icon:9});
            })
        }, function(){
        });
    });

    $("#list-data").on("click",".topic-pass",function(){
        var data = $.parseJSON($(this).attr("data"));
        layer.confirm('是否通过审核？', {
            btn: ['通过','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            var posting = $.post($CONFIG.base_url + "/api/topic/educator/pass", data);
            posting.done(function(data) {
                if (data == "true") {
                    layer.msg('成功', {icon: 1});
                    window.location.href = $CONFIG.base_url + "/admin/topic/manage";
                }
                if (data == "tch_wait") {
                    layer.msg('等待教师审核', {
                        icon: 9,
                        time: 2000
                    });
                }
                if (data == "tch_false") {
                    layer.msg('没有通过教师审核', {
                        icon: 9,
                        time: 2000
                    });
                }
                if (data == "ech_false") {
                    layer.msg('没有通过教务审核', {
                        icon: 9,
                        time: 2000
                    });
                }
                if (data == "edu_success") {
                    layer.msg('已经通过教务审核了', {
                        icon: 9,
                        time: 2000
                    });
                }
            });
        }, function(){
        });
    });

    //显示详情模板
    exports.loadList1 = function(param){
        var list = new List();
        list.options = {
            template : "list-template1",
            container : "list-data1",
            url : $CONFIG.base_url + "/api/topic/details",
            param : param
        };
        list.load();
    };

});
